using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Source;

namespace OR.Lazarus.Library.DataTier.Source.Lazarus
{
	/// <summary>
	/// Defines a constraint on a field.  This limits the set of valid values.
	/// </summary>
   public class ConstraintDetail : EntityBase
   {
		protected System.String mName;
		protected string mModifier;
		protected string mOperation;
		protected string mValue;
		protected string mMinValue;
		protected string mMaxValue;
		protected string mDescription;
	
		protected Derived.ConstraintValueCollection mValues;

		public ConstraintDetail()
		{
			mValues = new Derived.ConstraintValueCollection();
			mValues.ParentEntity = this;
	
		}
	
		/// <summary>
		/// Specifies a unique name for the entity.
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// Specifies the type of constraint condition that will be checked.  Valid modifiers are Default (normal comparison) and Not (constraint operates on the negative of the comparison).
		/// </summary>
		public string Modifier
		{
			get
			{
				return ( mModifier );
			}
			set
			{
				mModifier = value;
			}
		}

		/// <summary>
		/// Specifies the constraint operation that will be performed.  Valid Operations are: GreaterThan: ( > ); GreaterThanOrEqual ( >= ), LessThan ( < ); LessThanOrEqual: ( <= ); ExclusiveBetween: ( BETWEEN [min] AND [max] ); InclusiveBetween: ( ( > [min] ) AND ( < [max] ) ); Equal: ( == ); In: IN ( [values] );
		/// </summary>
		public string Operation
		{
			get
			{
				return ( mOperation );
			}
			set
			{
				mOperation = value;
			}
		}

		/// <summary>
		/// Specifies a single, scalar value.  This property is used in Operations where a single value is being compared.  Ex: GreaterThan, Equal.
		/// </summary>
		public string Value
		{
			get
			{
				return ( mValue );
			}
			set
			{
				mValue = value;
			}
		}

		/// <summary>
		/// Specifies a minimum value.  This property is used for exclusive and inclusive Between operations.
		/// </summary>
		public string MinValue
		{
			get
			{
				return ( mMinValue );
			}
			set
			{
				mMinValue = value;
			}
		}

		/// <summary>
		/// Specifies a maximum value.  This property is used for exclusive and inclusive Between operations.
		/// </summary>
		public string MaxValue
		{
			get
			{
				return ( mMaxValue );
			}
			set
			{
				mMaxValue = value;
			}
		}

		/// <summary>
		/// Provides a long-text description for the entity.
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.ConstraintValueCollection Values
		{
			get
			{
				return ( mValues );
			}
			set
			{
				mValues = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mModifier = DataUtility.GetString( pSource, "modifier" );
			mOperation = DataUtility.GetString( pSource, "operation" );
			mValue = DataUtility.GetString( pSource, "value" );
			mMinValue = DataUtility.GetString( pSource, "min-value" );
			mMaxValue = DataUtility.GetString( pSource, "max-value" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vValues = ( XmlElement )pSource.SelectSingleNode( "lz:constraint-values", PersistenceConfig.Namespaces );
			
			if ( vValues != null )
			{
				mValues.LoadXml( vValues );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lz", "constraint", "http://schemas.icitadel.com/Lazarus/Source.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mModifier ) )
			{
				vNewElement.SetAttribute( "modifier", mModifier );
			}
					
			if ( !String.IsNullOrEmpty( mOperation ) )
			{
				vNewElement.SetAttribute( "operation", mOperation );
			}
					
			if ( !String.IsNullOrEmpty( mValue ) )
			{
				vNewElement.SetAttribute( "value", mValue );
			}
					
			if ( !String.IsNullOrEmpty( mMinValue ) )
			{
				vNewElement.SetAttribute( "min-value", mMinValue );
			}
					
			if ( !String.IsNullOrEmpty( mMaxValue ) )
			{
				vNewElement.SetAttribute( "max-value", mMaxValue );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lz", "description", "http://schemas.icitadel.com/Lazarus/Source.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mValues.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.ConstraintDetail v_oReturn = new Derived.ConstraintDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Modifier = mModifier;
			v_oReturn.Operation = mOperation;
			v_oReturn.Value = mValue;
			v_oReturn.MinValue = mMinValue;
			v_oReturn.MaxValue = mMaxValue;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.ConstraintValueDetail vDetail in mValues )
			{
				v_oReturn.Values.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.ConstraintDetail CloneTyped()
		{
			return ( ( Derived.ConstraintDetail )this.Clone() );
		}
   }
}
   